<template>
  <div class="editForm-wrapper">
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" status-icon :rules="dataRule" v-loading="loading" ref="inputForm" @keyup.enter.native="inputFormSubmit()"
             label-width="123px" @submit.native.prevent>
      <el-form-item label="角色名称:" prop="name" :rules="[
            {required: true, message: '角色名称不能为空', trigger: 'blur'}
          ]">
        <el-input v-model="inputForm.name" placeholder="角色名称" maxlength="50"></el-input>
        <input type="hidden" v-model="inputForm.oldName"/>
      </el-form-item>
      <el-form-item label="英文名称:" prop="enname" :rules="[
            {required: true, message: '角色英文名称不能为空', trigger: 'blur'}
          ]">
        <el-input v-model="inputForm.enname" maxlength="50" placeholder="角色英文名称"></el-input>
        <input type="hidden" v-model="inputForm.oldEname"/>
      </el-form-item>
      <el-form-item label="是否系统数据:" prop="sysData" :rules="[
            {required: true, message: '是否系统数据不能为空', trigger: 'blur'}
          ]">
        <el-select clearable v-model="inputForm.sysData" placeholder="请选择"  style="width: 100%;">
          <el-option
            v-for="item in this.$dictUtils.getDictList('yes_no')"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <div class="help-block">“是”代表此数据只有超级管理员能进行修改</div>
      </el-form-item>
      <el-form-item label="是否可用:" prop="useable" :rules="[
            {required: true, message: '是否可用不能为空', trigger: 'blur'}
          ]">
        <el-select clearable v-model="inputForm.useable" placeholder="请选择"  style="width: 100%;">
          <el-option
            v-for="item in this.$dictUtils.getDictList('yes_no')"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <div class="help-block">“是”代表此数据可用，“否”则表示此数据不可用</div>
      </el-form-item>
      <el-form-item class="marks" label="备注:" prop="remark">
        <el-input type="textarea" v-model="inputForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="inputFormSubmit()" v-noMoreClick>确定</el-button>
      <el-button @click="visible = false">取消</el-button>

    </span>
  </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        title: '',
        inputForm: {
          id: '',
          name: '',
          enname: '',
          remark: '',
          oldName: '',
          oldEname: '',
          sysData: '1',
          useable: '1'
        },
        dataRule: {
          name: [
            {required: true, message: '角色名称不能为空', trigger: 'blur'}
          ],
          enname: [
            {required: true, message: '角色英文名称不能为空', trigger: 'blue'}
          ]
        }
      }
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新增角色`
        } else if (method === 'edit') {
          this.title = '修改角色'
        } else if (method === 'view') {
          this.title = '角色详情'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (this.inputForm.id) {
            this.loading = true
            let p3 = this.$http({
              url: `/sys/role/queryById?id=${this.inputForm.id}`,
              method: 'get'
            })
            Promise.all([p3]).then((result) => {
              let data = result[0].data
              if (data && data.success) {
                this.inputForm = this.recover(this.inputForm, data.result)
                this.inputForm.oldName = data.result.name
                this.inputForm.oldEnname = data.result.enname
                this.loading = false
              } else {
                this.loading = false
                this.visible = false
              }
            })
          }
        })
      },
      // 表单提交
      inputFormSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/role/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.$message.success(data.msg)
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .editForm-wrapper{
    ::v-deep.el-dialog{
      .el-form{
        .help-block{
          font-size: 12px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #FF3E3E;
        }
        .el-form-item.marks{
          margin-bottom: 0 !important;
        }
      }
    }
  }
</style>
